<template>
  <div class='ele-body ele-body-card'>
    <a-card title="设置"
            :bordered='true'
            :body-style="{padding: '20px'}">
      <a-form
        ref='reportCycleformRef'
        :model='reportCycleformModel'
        :rules="reportCycleRules"
        :label-col='{ md: { span: 16 }, sm: { span: 24 } }'
        :wrapper-col='{ md: { span: 8 }, sm: { span: 24 } }'
        @submit='setReportCycle'
      >
        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='数据上报周期(秒):' name='report_cycle'>
              <a-input v-model:value='reportCycleformModel.report_cycle' placeholder='请输入数据上报周期' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='2' :sm='24' :xs='24'>
            <a-button type='primary' html-type='submit' :loading='reportCycleformRefLoading'>
              提交
            </a-button>
          </a-col>
          <a-col :md='6' :sm='24' :xs='24' />
        </a-row>
      </a-form>
    </a-card>
  </div>
</template>

<script>
import {DeviceCattleHeartbeatApi } from '@/api/property/cattlehealth/DeviceCattleHeartbeatApi';
import {DeviceCommandDownQueueApi} from '@/api/property/DeviceCommandDownQueueApi';
import { message } from 'ant-design-vue';

export default {
  name: 'DeviceCattleHeartbeatConfigOther',
  components: {
  },
  props: {
    data: Object
  },
  async created() {
    await this.init();
  },
  data() {
    return {
      loading : false,
      heartbeatDevice : {},

      reportCycleformModel : {},
      reportCycleformRefLoading : false,
      reportCycleformRefTimer : undefined
    };
  },
  methods: {
    getDetail() {
      let that = this;
      DeviceCattleHeartbeatApi.detailByRecord(this.data.recordId).then(result => {
        let _heartbeatDevice = result || {};
        that.heartbeatDevice = _heartbeatDevice;
        that.reportCycleformModel.report_cycle = _heartbeatDevice.reportCycle;
      });
    },
    async init() {
      this.getDetail();
    },

    checkProcess(dictate, clearCallback, callback) {
      let params = {
        recordId: this.data.recordId,
        sceneDictate: dictate
      };
      let result = DeviceCommandDownQueueApi.workingDetail(params);
      result
        .then(result => {
          if (!result || !result.dcdqId) { //处理完成了
            clearCallback && clearCallback(); //去掉菊花+清除循环调用
            callback && callback(); //用户自定义
          }
        })
        .catch(() => {
          clearCallback && clearCallback(); //去掉菊花+清除循环调用
        });
    },

    //==============设置周期==========================
    setReportCycle() {
      this.loading = true;
      let updateDataRecord = {
        recordId: this.data.recordId,
        templateCode: this.data.templateCode,
        updateData: JSON.stringify({ old: this.heartbeatDevice, update: this.reportCycleformModel}),
        dictate : 3,
        sceneDictate : 3
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.reportCycleformRefLoading = true; //转圈
          this.reportCycleformRefTimer = setInterval(this.checkReportCycle, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    },
    checkReportCycle(){
      let that = this;
      this.checkProcess(3, this.clearReportCycleformRef, function(){
        that.init();
      });
    },
    clearReportCycleformRef(){
      this.reportCycleformRefLoading = false;
      clearInterval(this.reportCycleformRefTimer);
    }
  }
};
</script>
<style scoped>
/** 用户卡片 */
.workplace-user-card .ele-cell-content {
  overflow: hidden;
}

.workplace-user-card h4 {
  margin-bottom: 6px;
}
</style>
